<?php
/**
 * @file americansearch.php
 * @author LinLin
 * @date 2016年4月7日 下午1:39:59
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\american;

use haibao\frame\data\query\Condition;
use \haibao\cms\business\americandrama\AmericanDefine as AmericanDefineBusiness;

class AmericanSearchController extends \haibao\cms\business\control\AmericanController{

    public function __construct($updateTime = null,$controlId,$isPublish){
         
        parent::__construct($updateTime,$controlId,$isPublish);
    }

    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);

        $hotSearch = array();
        if($controlData['manual'] && $controlData['manual']['controllerType'] == 'hotSearch'){
            if($controlData['auto']){
                $temp = array();
                foreach($controlData['auto'] as $key=>$data){
                    $hotSearch[$key]['hotUsName'] =json_decode($data['ParameterValue'])->hotUsName;
                    $hotSearch[$key]['enCode'] = rawurlencode($hotSearch[$key]['hotUsName']);
                }
            }
            $this->assign('hotSearch', $hotSearch);
            $this->assign('sourceType', $controlData['manual']['sourceType']);
        }
    
        
        if($controlData['manual'] && $controlData['manual']['controllerType'] == 'searchAmerican'){
            $mjType = \haibao\cms\business\americandrama\AmericanDefine::$meiju_jmtype_arr;
            $mjTVStation = \haibao\cms\business\americandrama\AmericanDefine::$meiju_dt_arr;
             
            $americanDefineFilter = new \haibao\cms\model\filter\americandrama\AmericanDefine();
            $americanDefineFilter->select(array(
                $americanDefineFilter::CONDITION_FIELD_NAME_ID,
                $americanDefineFilter::CONDITION_FIELD_NAME_NAMECN,
                $americanDefineFilter::CONDITION_FIELD_NAME_NAMEEN,
                $americanDefineFilter::CONDITION_FIELD_NAME_AVATARURL
            ))->limit(20);
            
            $americanDefineFilter->where($americanDefineFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\americandrama\AmericanDefine::STATUS_ENABLE);
            //$americanDefineFilter->where($americanDefineFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL, \haibao\cms\model\data\americandrama\AmericanDefine::PUBLISHED_STATUS_ENABLE);
            $americanDefineFilter->where($americanDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,array(
                new Condition($americanDefineFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\americandrama\AmericanDefine::PUBLISHED_STATUS_ENABLE),
                new Condition($americanDefineFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\americandrama\AmericanDefine::PUBLISHED_STATUS_ING)
            ));
            
            
            $americanCategoryFilter = new \haibao\cms\model\filter\americandrama\AmericanCategory();
            $americanCategoryFilter->select(array(
                $americanCategoryFilter::CONDITION_FIELD_NAME_ID,
                $americanCategoryFilter::CONDITION_FIELD_NAME_AMERICANID,
                $americanCategoryFilter::CONDITION_FIELD_NAME_CATEGORYID
            ));
            
            $americanDefineFilter->leftJoin($americanCategoryFilter,null,$americanDefineFilter::CONDITION_FIELD_NAME_ID,$americanCategoryFilter::CONDITION_FIELD_NAME_AMERICANID,$americanDefineFilter::CONDITION_FIELD_NAME_AMERICANCATEGORY);
           
            $americanDefineFilter->order($americanDefineFilter::CONDITION_FIELD_NAME_UPDATETIME,false);
            $americanInfoLists = \haibao\cms\business\americandrama\AmericanDefine::getAmericanDramaLists($americanDefineFilter);
    
            $americanInfoArr =  $categoryArr = array();
            foreach($americanInfoLists as $key=>$val){
                if($val->AvatarUrl){
                    $americanInfoLists[$key]->AvatarUrl = self::getImageLink($val->AvatarUrl,145,210);
                }
                foreach($val->AmericanCategory as $k=>$v){
                    if(isset($mjType[$v->CategoryId])){
                       $val->AmericanCategory[$k]->CategoryName = $mjType[$v->CategoryId];
                    }
                }
                $americanInfoLists[$key]->AmericanUrl = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_TV_HOST).'meiju/'.$val->Id.'/';
            }
            
            if($controlData['manual']['sourceType'] && $controlData['manual']['sourceType']=='wapMultipleSearch'){
                $this->baseUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/tv/ku/list_';
            }elseif ($controlData['manual']['sourceType'] && $controlData['manual']['sourceType']=='pcMultipleSearch'){
                $this->baseUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_TV_HOST).'ku/list_';
            }
            
            $paramArr = array();
            
            $paramArr = array_merge($paramArr,array('type'=>0,'station'=>0,'date'=>0,'letter'=>0,'reserve'=>0,'stay'=>0,'page'=>1));
        
            $this->getSearchAllUrl($paramArr,$controlData['manual']['sourceType']);
            $dateList = array();
            if($controlData['manual']['sourceType'] == 'wapMultipleSearch'){
                $dateList = \haibao\cms\business\americandrama\AmericanDefine::$meiju_date_arr;
                $dateList[11]='2006-2000';
                foreach ($dateList as $key=>$value)
                {
                    if($value=='90年代'){
                        $dateList[$key] = "90's";
                    }elseif ($value=='80年代'){
                        $dateList[$key] = "80's";
                    }elseif ($value=='70年代'){
                        $dateList[$key] = "70's";
                    }elseif ($value=='60年代'){
                        $dateList[$key] = "60's";
                    }
                     
                    if ($key >=12 && $key<=17)
                        unset($dateList[$key]);
                }
               
            }
            $this->assign('mjDate', $dateList);
            $this->assign('mjType', $mjType);
            $this->assign('mjTVStation', $mjTVStation);
            $this->assign('americanInfoLists', $americanInfoLists);
            $this->assign('multipleSourceSearchType', $controlData['manual']['sourceType']);
        }
        
        $parameters = $this->getControlParameters($controlData);
        $this->assign('parameters', $parameters);
        $this->assign('userHostColler', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/search/index.html?name=');
        $this->assign('wapUserHostColler', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/search/index.html?name=');
        $this->generateHtml($path, $content);
        return $this->getHtml($path);
    }
    

private function getSearchAllUrl($condition,$sourceType){
        $americanTypeList = $americanStationList = $americanDateList  = $americanLetterList = array();
        foreach(AmericanDefineBusiness::$meiju_jmtype_arr as $key=>$type){
            $temp['name'] = $type;
            $temp['url'] = $this->baseUrl.$key.'_'.$condition['station'].'_'.$condition['date'].'_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/';
            array_push($americanTypeList,$temp);
        }
        array_unshift($americanTypeList,array('name'=>'全部','url'=>$this->baseUrl.'0_'.$condition['station'].'_'.$condition['date'].'_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
        
        foreach(AmericanDefineBusiness::$meiju_dt_arr as $key=>$station){
            $temp['name'] = $station;
            $temp['url'] = $this->baseUrl.$condition['type'].'_'.$key.'_'.$condition['date'].'_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/';
            $americanStationList[$key] = $temp;
        }
        array_unshift($americanStationList,array('name'=>'全部','url'=>$this->baseUrl.$condition['type'].'_0_'.$condition['date'].'_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
        
        $nowYear = date('Y',time());
        $showDate = AmericanDefineBusiness::$meiju_date_arr;
        $dValue = $nowYear-$showDate[1];
        if($dValue>=1){
            for($i=1;$i<=$dValue;$i++){
                array_unshift($showDate, $showDate[0]+1);
            }
        }
        foreach($showDate as $key=>$date){
            $temp['name'] = $date;
            if($sourceType == 'pcMultipleSearch'){
                $temp['url'] = $this->baseUrl.$condition['type'].'_'.$condition['station'].'_'.$key.'_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/';
            }elseif ($sourceType == 'wapMultipleSearch'){
                $temp['url'] = $this->baseUrl.$condition['type'].'_'.$condition['station'].'_'.$key.'_'.$condition['letter'].'_1_'.$condition['stay'].'/';
            }
            $americanDateList[$key] = $temp;
        }
        array_unshift($americanDateList,array('name'=>'全部','url'=>$this->baseUrl.$condition['type'].'_'.$condition['station'].'_0_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
    
        for($start=ord("A");$start<=ord("Z");$start++){
            $temp['choice'] = false;
            if(strlen($condition['letter']) == 1 && ord($condition['letter']) == $start){
                $temp['choice'] = true;
            }
            $temp['name'] = chr($start);
            $temp['url'] = $this->baseUrl.$condition['type'].'_'.$condition['station'].'_'.$condition['date'].'_'.chr($start).'_'.$condition['reserve'].'_'.$condition['stay'].'/';
            array_push($americanLetterList,$temp);
        }
        
        $moreTypeBefore = array_slice($americanTypeList,16,5);
        foreach($americanTypeList as $key=>$station){
            foreach($moreTypeBefore as $k=>$v){
                if($station['name'] == $v['name']){
                    unset($americanTypeList[$key]);
                }
            }
        }
        
        $moreStarionBefore = array_slice($americanStationList,9,6);
        foreach($americanStationList as $key=>$station){
            foreach($moreStarionBefore as $k=>$v){
                if($station['name'] == $v['name']){
                    unset($americanStationList[$key]);
                }
            }
        }
        
        
        $moreBefore = array_slice($americanDateList,-13);
        foreach($americanDateList as $key=>$date){
            foreach($moreBefore as $k=>$v){
                if($date['name'] == $v['name']){
                    unset($americanDateList[$key]);
                }
            }
        }
        array_push($americanDateList,array('name'=>'更早','url'=>$this->baseUrl.$condition['type'].'_'.$condition['station'].'_0_'.$condition['letter'].'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
    
        if($condition['letter'] === \haibao\cms\business\control\AmericanController::AMERICAN_A_Z_OTHER_LETTER){
            array_push($americanLetterList,array('choice'=>true,'name'=>'其他','url'=>$this->baseUrl.$condition['type'].'_'.$condition['station'].'_'.$condition['date'].'_'.\haibao\cms\business\control\AmericanController::AMERICAN_A_Z_OTHER_LETTER.'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
        }else{
            array_push($americanLetterList,array('choice'=>false,'name'=>'其他','url'=>$this->baseUrl.$condition['type'].'_'.$condition['station'].'_'.$condition['date'].'_'.\haibao\cms\business\control\AmericanController::AMERICAN_A_Z_OTHER_LETTER.'_'.$condition['reserve'].'_'.$condition['stay'].'/'));
        }
    
        $this->assign('americanTypeList',$americanTypeList);
        $this->assign('americanStationList',$americanStationList);
        $this->assign('americanDateList',$americanDateList);
        $this->assign('americanLetterList',$americanLetterList);
    }

} 